package com.eboss.sbs.activity;

import android.os.Bundle;
import android.view.View;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.AccountInfoListAdapter;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.AccountInfoEntity;
import com.eboss.sbs.model.TerminalDetailEntity;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TerminalDetailActivity extends BaseActivity {

	public static String TERMINALID = "TERMINALID";
	
	private int terminalId;
	
	private MyListView listview;
	private AccountInfoListAdapter adapter;
	private List<AccountInfoEntity> datas;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_terminal_detail);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "智能终端管详情");
		
		terminalId = getIntent().getIntExtra(TERMINALID, -1);
		
		datas = new ArrayList<AccountInfoEntity>();
		listview = (MyListView)findViewById(R.id.listview);
		adapter = new AccountInfoListAdapter(this, datas);
		
		listview.setFocusable(false);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		
		listview.setAdapter(adapter);
		
		getdata();
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("terminalId", terminalId);
		
		API.http_post(this, API.INTEL_TERMINAL_DETAIL, params, new HttpCallback<TerminalDetailEntity>(this) {
			@Override
			public void onSuccess(int statusCode, TerminalDetailEntity data) {
				if(data!=null){
					String terminalState = data.getTerminalState();
					showView(R.id.next_sure, !StringUtil.isNull(terminalState));
					setTvText(R.id.next_sure, StringUtil.isNull(terminalState)?"":terminalState);
					setTvText(R.id.tv_terminal_name, data.getTerminalName());
					setTvText(R.id.tv_superiors, data.getSuperiors());
					setTvText(R.id.tv_address, data.getAddress());
					setTvText(R.id.tv_merchant_no, data.getMerchantNo());
					setTvText(R.id.tv_terminal_no, data.getTerminalNo());
					setTvText(R.id.tv_company_name, data.getCompanyName());
					setTvText(R.id.tv_company_address, data.getCompanyAddress());
					setTvText(R.id.tv_license_no, data.getLicenseNo());
					setTvText(R.id.tv_account_name, data.getAccountName());
					setTvText(R.id.tv_account_bank, data.getAccountBank());
					setTvText(R.id.tv_account_no, data.getAccountNo());
					setTvText(R.id.tv_finance_contact, data.getFinanceContact());
					setTvText(R.id.tv_contact_phone, data.getContactPhone());
					
					 List<AccountInfoEntity> tmp_datas = data.getAccounts();
					 datas.clear();
					 if(tmp_datas!=null&&tmp_datas.size()>0){
						 datas.addAll(tmp_datas);
					 }
				}
				adapter.notifyDataSetChanged();
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<TerminalDetailEntity>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
	
			default:break;
		}
		super.onClick(v);
	}
	
}
